<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" />
    <title>Infinite Scroll - Basic</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
    <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
    <script src="../../../../../scripts/testing/scripts.js"></script>
    <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
    <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
  </head>

  <body>
    <ion-app>
      <ion-header>
        <ion-toolbar>
          <ion-title>Infinite Scroll - Basic</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-content class="ion-padding" id="content">
        <ion-button onclick="toggleInfiniteScroll()" expand="block"> Toggle InfiniteScroll </ion-button>

        <ion-list id="list"></ion-list>

        <ion-infinite-scroll threshold="100px" id="infinite-scroll">
          <ion-infinite-scroll-content loading-spinner="crescent" loading-text="Loading more data...">
          </ion-infinite-scroll-content>
        </ion-infinite-scroll>
      </ion-content>
    </ion-app>

    <script>
      const list = document.getElementById('list');
      const infiniteScroll = document.getElementById('infinite-scroll');

      function toggleInfiniteScroll() {
        infiniteScroll.disabled = !infiniteScroll.disabled;
      }

      infiniteScroll.addEventListener('ionInfinite', async function () {
        await wait(500);
        infiniteScroll.complete();
        appendItems();
        // Custom event consumed in the e2e tests
        window.dispatchEvent(new CustomEvent('ionInfiniteComplete'));
      });

      function appendItems() {
        for (var i = 0; i < 30; i++) {
          const el = document.createElement('ion-item');
          el.textContent = `${1 + i}`;
          list.appendChild(el);
        }
      }

      function wait(time) {
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve();
          }, time);
        });
      }

      appendItems();
    </script>
  </body>
</html>
